home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / pynche / Switchboard.py < prev    next >
Encoding:
Python Source  |  2000-06-23  |  3.0 KB  |  100 lines

  1. """Switchboard class.
  2.  
  3. This class is used to coordinate updates among all Viewers.  Every Viewer must
  4. conform to the following interface:
  5.  
  6.     - it must include a method called update_yourself() which takes three
  7.       arguments; the red, green, and blue values of the selected color.
  8.  
  9.     - When a Viewer selects a color and wishes to update all other Views, it
  10.       should call update_views() on the Switchboard object.  Not that the
  11.       Viewer typically does *not* update itself before calling update_views(), 
  12.       since this would cause it to get updated twice.
  13. """
  14.  
  15. import sys
  16. from types import DictType
  17. import marshal
  18.  
  19. class Switchboard:
  20.     def __init__(self, colordb, initfile):
  21.         self.__initfile = initfile
  22.         self.__colordb = colordb
  23.         self.__optiondb = {}
  24.         self.__views = []
  25.         self.__red = 0
  26.         self.__green = 0
  27.         self.__blue = 0
  28.         self.__canceled = 0
  29.         # read the initialization file
  30.         fp = None
  31.         if initfile:
  32.             try:
  33.                 try:
  34.                     fp = open(initfile)
  35.                     self.__optiondb = marshal.load(fp)
  36.                     if type(self.__optiondb) <> DictType:
  37.                         sys.stderr.write(
  38.                             'Problem reading options from file: %s\n' %
  39.                             initfile)
  40.                         self.__optiondb = {}
  41.                 except (IOError, EOFError):
  42.                     pass
  43.             finally:
  44.                 if fp:
  45.                     fp.close()
  46.  
  47.     def add_view(self, view):
  48.         self.__views.append(view)
  49.  
  50.     def update_views(self, red, green, blue):
  51.         self.__red = red
  52.         self.__green = green
  53.         self.__blue = blue
  54.         for v in self.__views:
  55.             v.update_yourself(red, green, blue)
  56.  
  57.     def update_views_current(self):
  58.         self.update_views(self.__red, self.__green, self.__blue)
  59.  
  60.     def current_rgb(self):
  61.         return self.__red, self.__green, self.__blue
  62.  
  63.     def colordb(self):
  64.         return self.__colordb
  65.  
  66.     def optiondb(self):
  67.         return self.__optiondb
  68.  
  69.     def save_views(self):
  70.         # save the current color
  71.         self.__optiondb['RED'] = self.__red
  72.         self.__optiondb['GREEN'] = self.__green
  73.         self.__optiondb['BLUE'] = self.__blue
  74.         for v in self.__views:
  75.             if hasattr(v, 'save_options'):
  76.                 v.save_options(self.__optiondb)
  77.         fp = None
  78.         try:
  79.             try:
  80.                 fp = open(self.__initfile, 'w')
  81.             except IOError:
  82.                 sys.stderr.write('Cannot write options to file: %s\n' %
  83.                                  self.__initfile)
  84.             else:
  85.                 marshal.dump(self.__optiondb, fp)
  86.         finally:
  87.             if fp:
  88.                 fp.close()
  89.  
  90.     def withdraw_views(self):
  91.         for v in self.__views:
  92.             if hasattr(v, 'withdraw'):
  93.                 v.withdraw()
  94.  
  95.     def canceled(self, flag=1):
  96.         self.__canceled = flag
  97.  
  98.     def canceled_p(self):
  99.         return self.__canceled
  100.